package com.ourhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ourhealth.entity.PageResult;
import com.ourhealth.mapper.CheckItemGroupMapper;
import com.ourhealth.pojo.CheckGroup;
import com.ourhealth.service.CheckItemGroupService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dddddh
 */
@Service(interfaceClass = CheckItemGroupService.class)
@Transactional(rollbackFor = Exception.class)
public class CheckItemGroupServiceImpl implements CheckItemGroupService {
    @Autowired
    private CheckItemGroupMapper checkItemGroupMapper;
    @Override
    public void addCheckItemGroup(CheckGroup checkGroup, Integer[] checkItemIds) {
        checkItemGroupMapper.addCheckItemGroup(checkGroup);
        addCheckItemIdAndCheckGroupId(checkGroup.getId(),checkItemIds);
    }

    @Override
    public void addCheckItemIdAndCheckGroupId(Integer checkGroupID, Integer[] checkItemIds) {
        List<Map> list = new ArrayList<>();
        for (int i = 0; i < checkItemIds.length; i++) {
            Map<String,Integer> map = new HashMap<>();
            map.put("checkGroup_id",checkGroupID);
            map.put("checkItem_id",checkItemIds[i]);
            list.add(map);
            System.out.println(map);
        }
        checkItemGroupMapper.addCheckGroupAndCheckItem(list);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkItemGroupMapper.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void editCheckItemGroup(CheckGroup checkGroup, Integer[] checkItemIds) {
        deleteCheckItemIdsAndCheckGroupId(checkGroup.getId());
        addCheckItemIdAndCheckGroupId(checkGroup.getId(),checkItemIds);
        checkItemGroupMapper.editCheckItemGroup(checkGroup);

    }

    @Override
    public void deleteCheckItemIdsAndCheckGroupId(Integer checkGroupId) {
        checkItemGroupMapper.deleteCheckItemIdsAndCheckGroupId(checkGroupId);
    }

    @Override
    public CheckGroup findCheckGroupById(Integer id) {
        return checkItemGroupMapper.findCheckGroupById(id);
    }

    @Override
    public Integer[] findChooseCheckItem(Integer id) {
        return checkItemGroupMapper.findChooseCheckItem(id);
    }

    @Override
    public void deleteCheckGroup(@Param("id") Integer id) {
        checkItemGroupMapper.deleteCheckItemIdsAndCheckGroupId(id);
        checkItemGroupMapper.deleteCheckGroup(id);
    }

    @Override
    public List<CheckGroup> finAll() {
        return checkItemGroupMapper.findAll();
    }

    @Override
    public List<Integer> findCheckItemIdByCheckItemGroupId(Integer integer) {
        return checkItemGroupMapper.findCheckItemIdByCheckItemGroupId(integer);
    }


}
